import 'package:cbec_app/components/decoration/decoration_goods.dart';
import 'package:cbec_app/components/decoration/group_nav_item.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'decoration_goods_group_ctr.dart';

///店铺装修-商品分组
class DecorationGoodsGroup extends StatefulWidget {
  final Map<String, dynamic> info;
  final String idKey;
  final int goodsType;

  const DecorationGoodsGroup({super.key, required this.info, this.idKey = 'id', this.goodsType = 0});

  @override
  State<StatefulWidget> createState() {
    return _DecorationGoodsGroupState();
  }
}

class _DecorationGoodsGroupState extends State<DecorationGoodsGroup> {
  late DecorationGoodsGroupCtr controller;

  @override
  void initState() {
    super.initState();
    String timestamp = '${DateTime.now().millisecondsSinceEpoch}_${UniqueKey()}';
    controller = Get.put(DecorationGoodsGroupCtr(), tag: timestamp);
  }

  @override
  Widget build(BuildContext context) {
    controller.fetchTabData(widget.info);

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Obx(() {
          return Column(
            children: [
              // Tab title section
              Container(
                width: ScreenUtil().screenWidth,
                color: Colors.white,
                margin: EdgeInsets.only(left: getMargin()),
                child: SingleChildScrollView(
                  scrollDirection: Axis.horizontal,
                  child: Row(
                    children: List.generate(controller.tabTitleModels.length, (index) {
                      return GroupNavItem(
                        info: widget.info,
                        item: controller.tabTitleModels[index].toJson(),
                        index: index,
                        curIndex: controller.curIndex.value,
                        callBack: (id, index, pageSize) {
                          controller.onSelectGoodsGroup(id, index, pageSize);
                        },
                        isShow: controller.tabTitleModels.length > 1,
                      );
                    }),
                  ),
                ),
              ),
              // const SizedBox(
              //   height: 10,
              // ),
              // DecorationGoods section
              DecorationGoods(
                info: widget.info,
                goodsList: controller.goodsList,
                idKey: widget.idKey,
                goodsType: widget.goodsType,
              ),
            ],
          );
        }),
      ],
    );
  }

  ///获取页面间距
  double getMargin() {
    return double.parse(widget.info['page_margin'].toString());
  }
}
